﻿using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TradingServer.Core.Entities;
using System.Threading;
using System.Threading.Tasks;

namespace UnitTests
{
	[TestClass]
	public class ClientOperations
	{
		Security Security;

		[TestInitialize]
		public void Startup()
		{
			Security = new Security("LKOH", 10);
		}

		[TestMethod]
		public void ClientBuy()
		{
			Client buyer = new Client(Guid.NewGuid());
			Client seller = new Client(Guid.NewGuid());

			Account buyerAccount = new Account(buyer);
			Account sellerAccount = new Account(seller);

			Order buyOrder = new Order(OrderType.Buy, Security, 10, 100);
			buyOrder.Account = buyerAccount;

			Order sellOrder = new Order(OrderType.Sell, Security, 10, 100);
			sellOrder.Account = sellerAccount;

			buyer.PlaceOrder(buyOrder);
			seller.PlaceOrder(sellOrder, TimeSpan.FromSeconds(1), true);

			Assert.AreEqual(1, buyerAccount.Briefcase.Items.Count());
			Assert.AreEqual(1, sellerAccount.Briefcase.Items.Count());
		}

		[TestMethod]
		public void ClientBuyTimedout()
		{
			Client buyer = new Client(Guid.NewGuid());
			Client seller = new Client(Guid.NewGuid());

			Account buyerAccount = new Account(buyer);
			Account sellerAccount = new Account(seller);

			Order buyOrder = new Order(OrderType.Buy, Security, 10, 100.10M);
			buyOrder.Account = buyerAccount;

			Order sellOrder = new Order(OrderType.Sell, Security, 10, 100.20M);
			sellOrder.Account = sellerAccount;

			buyer.PlaceOrder(buyOrder, TimeSpan.FromSeconds(1), true);
			seller.PlaceOrder(sellOrder, TimeSpan.FromSeconds(1), true);

			Assert.AreEqual(0, buyerAccount.Briefcase.Items.Count());
			Assert.AreEqual(0, sellerAccount.Briefcase.Items.Count());
		}
	}
}
